<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * Класс роутинга
 * Расширяет соответствующий стандартный класс
 * 
 * @package CoreFramework
 * @version 1.0
 * @author Gennadiy Kozlenko
 */
class CF_Router extends CI_Router {
   
   /**
    * Validates the supplied segments.  Attempts to determine the path to
    * the controller.
    *
    * @access  private
    * @param   array
    * @return  array
    */   
   function _validate_request($segments)
   {
      // Does the requested controller exist in the root folder?
      if (file_exists(APPPATH.'controllers/'.$segments[0].EXT))
      {
         return $segments;
      }

      // Is the controller in a sub-folder?
      if (is_dir(APPPATH.'controllers/'.$segments[0]))
      {     
         // Set the directory and remove it from the segment array
         $this->set_directory($segments[0]);
         $segments = array_slice($segments, 1);
         
         if (count($segments) > 0)
         {
            // Does the requested controller exist in the sub-folder?
            if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$segments[0].EXT))
            {
               //show_404($this->fetch_directory().$segments[0]);
               return $this->custom_404();
            }
         }
         else
         {
            $this->set_class($this->default_controller);
            $this->set_method('index');
         
            // Does the default controller exist in the sub-folder?
            if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$this->default_controller.EXT))
            {
               $this->directory = '';
               return array();
            }
         
         }

         return $segments;
      }

      // Can't find the requested controller...
      //show_404($segments[0]);
      return $this->custom_404();
   }

   /**
    * Возвращяет массив с контроллером - обработчиком 404 ошибки
    * @return array
    */
   function custom_404() {
      return array('error_404');
   }

}
